package com.junjie.demo1;

import java.util.Random;

/**
 * if与switch性能损耗分析代码
 */
public class Test01 {

    public static void main(String[] args) {
        //生成一个随机数
        Random random = new Random();
        int i = random.nextInt(4);

        System.out.println(newIntence(i));

        System.out.println(newObject(i));
    }

    //if方式生成
    public static Object newIntence(int temp){
        if (temp == 0){
            return new Animal();
        }else if(temp == 1){
            return new Cat();
        }else if(temp == 2){
            return new Dog();
        }else if(temp == 3){
            return new Pig();
        }else {
            return null;
        }
    }

    //switch方式生成
    public static Object newObject(int temp){
        switch(temp){
            case 0:
                return new Animal();
            case 1:
                return new Cat();
            case 2:
                return new Dog();
            case 3:
                return new Pig();
            default:
                return null;
        }
    }
}
